﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.ComponentModel;
using System.Xml.Linq;
using Android.App;
using Android.Content;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using ComApp.Model;
using ComApp;
using Android.Widget;
using Android.Views;

namespace AndroidApp
{
    public class PopMenu
    {
        private List<String> itemListStr;
        //private List<int> itemListInt;
        private Context context;
        private PopupWindow popupWindow;
        public ListView listView;

        public PopMenu(Context context, List<string> menuItem)
        {
            this.context = context;
            itemListStr = menuItem;
            //itemListInt = new List<int>();
            //int[] i=new int[]{R.drawable.picture0,R.drawable.picture1,R.drawable.picture2,R.drawable.picture3};  
            //for(int img:i){  
            //    Integer listImgInteger=Integer.valueOf(img);//int-->Integer  
            //    itemListInt.add(listImgInteger);  
            //}  
            View view = LayoutInflater.From(context).Inflate(Resource.Layout.PopupMenuList, null);//PopupWindow里面的view  
            listView = (ListView)view.FindViewById(Resource.Id.menu_listview);
            listView.SetAdapter(new PopMenuAdapter(context, itemListStr /*,itemListInt*/));

            listView.FocusableInTouchMode = true;
            listView.Focusable = true;
            popupWindow = new PopupWindow(view, 300, Android.Widget.RelativeLayout.LayoutParams.WrapContent);
            popupWindow.SetBackgroundDrawable(new Android.Graphics.Drawables.BitmapDrawable()); // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景（很神奇的）,  
        }

        // 下拉式 弹出 pop菜单 parent 右下角  
        public void ShowAsDropDown(View parent)
        {
            //showAsDropDown(View anchor, int xoff, int yoff)弹出对话框，位置在紧挨着view组件，x y 代表着偏移量   
            popupWindow.ShowAsDropDown(parent, 0 , 0 + 1);
            popupWindow.Focusable = true;//必须要获得焦点哦，不然里面的控件啥的点击无效的哦  
            popupWindow.OutsideTouchable = true;// 设置允许在外点击消失  
            popupWindow.Update();   // 刷新状态  
        }

        public void ShowAsDropUp(View parent)
        {
            //popupWindow.ShowAtLocation(parent, GravityFlags.Left, (int)parent.GetX(), (int)parent.GetY() - 200);
            //AppUnity.Instance.LogService.Log(string.Format("popupWindow.Height={0}", popupWindow.Height));
            popupWindow.ShowAsDropDown(parent, 0, -(int)parent.Height - 110);
            popupWindow.Focusable = true;//必须要获得焦点哦，不然里面的控件啥的点击无效的哦  
            popupWindow.OutsideTouchable = true;// 设置允许在外点击消失  
            popupWindow.Update();   // 刷新状态 
        }

        // 隐藏菜单  
        public void Dismiss()
        {
            popupWindow.Dismiss();
        }

    }
}
